MODULE ModFuncs
  
!!$ This module contains frequently used functions.
!!$ TODO: Combine modparms and modfuncs into modcommon
!!$ Program conventions are from Nyhoff and Leestma, _Fortran 90 for Scientists and Engineers_, 1997
!!$
!!$ STH: Samuel Harrold
  
!!$ Declare modules to USE
  USE ModParms
  
!!$ Do not assume undeclared variables
  IMPLICIT NONE


  
CONTAINS
  
  
  
!!$ Bnu
!!$ Blackbody function with respect to frequency.
!!$ Accepts: Frequency in Hz, temperature in K
!!$ Returns: Blackbody flux density at the specified freqency in units of erg s^-1 cm^-2 sr^-1 Hz^-1 
  
  FUNCTION Bnu(nu, T)
    
!!$ Specify output and input
!!$ Bnu = blackbody flux density in erg s^-1 cm^-2 sr^-1 Hz^-1
!!$ nu  = frequency in Hz
!!$ T   = temperature in K
    REAL :: Bnu
    REAL, INTENT(IN) :: nu, T
    
    Bnu = (2.*h*nu**3)/(c**2) * 1./(EXP((h*nu)/(k*T)) - 1.)
    
  END FUNCTION Bnu
  
  
  
!!$ InterpLin
!!$ Interpolate values linearly between two coordinate pairs.
!!$ Accepts: two coordinate pairs (xi, yi), (xf, yf), i = initial, f = final
!!$ Returns: linearly interpolated value between the coordinate pairs
  
  FUNCTION InterpLinear(x, xi, yi, xf, yf)
    
!!$ Specify output and input
!!$ InterpLinear = interpolated dependent value cooresponding to x
!!$ x            = independent value at which interpolation is to be calculated
!!$ (x/y)(i/f)   = indepedent/dependent value at initial/final coordinate point to determine interpolation curve
    REAL :: InterpLinear
    REAL, INTENT(IN) :: x, xi, yi, xf, yf
    
!!$ Declare internal variables
!!$ SlopeLinear  = slope between the two coordinate pairs (xi, yi), (xf, yf)
    REAL :: SlopeLinear

    SlopeLinear  = (yf - yi)/(xf - xi)
    InterpLinear = SlopeLinear*(x - xi) + yi

  END FUNCTION InterpLinear



!!$ InterpLog
!!$ Interpolate values logarithmically between two coordinate pairs.
!!$ Accepts: two coordinate pairs (xi, yi), (xf, yf), i = initial, f = final
!!$ Returns: logarithmically interpolated value between the coordinate pairs

  FUNCTION InterpLog(x, xi, yi, xf, yf)

!!$ Specify output and input
!!$ InterpLog  = interpolated dependent value cooresponding to x
!!$ x          = independent value at which interpolation is to be calculated
!!$ (x/y)(i/f) = indepedent/dependent value at initial/final coordinate point to determine interpolation curve
    REAL :: InterpLog
    REAL, INTENT(IN) :: x, xi, yi, xf, yf

!!$ Declare internal variables
!!$ SlopeLog  = slope between the two coordinate pairs (xi, yi), (xf, yf)
    REAL :: SlopeLog

    SlopeLog  = LOG10(yf/yi) / LOG10(xf/xi)
    InterpLog = (x/xi)**SlopeLog * yi

  END FUNCTION InterpLog



END MODULE ModFuncs
